<template>
	<view class="page" style="padding-bottom: 50rpx;">
		<view class="topVue">
			<view class="safeVue" :style="{ height: statusBarHeight + 'rpx' }"></view>
			<view class="flex align-center padding-row30" :style="{ height: barHeight + 'rpx', top: statusBarHeight + 'rpx' }" @click="$tools.back()">
				<image src="../../static/back.png" style="width: 48rpx; height: 48rpx;"></image>
				<text style="line-height: 1; color: #454545; font-weight: 600; font-size: 32rpx; padding-left: 20rpx;">红包兑换</text>
				<view class="opBtn" @click.stop="$tools.route('./pocketRecord')">兑换记录</view>
			</view>
		</view>
		<view :style="{ height: statusBarHeight + barHeight + 'rpx' }"></view>
		
		<view class="box padding30 margin30">
			<view class="balance" style="font-weight: 600;">红包余额： {{ user.point }}</view>
			
			<view class="balance">选择兑换数量</view>
			<view class="flex-wrap flex-center margin30">
				<view class="li" :class="{active: numId===item.id}" v-for="(item, index) in nums" :key="index" @click="num=item.num;numId=item.id">{{item.num}}</view>
			</view>
			
			<view class="exchangeBtn margin50" @click="submit()">兑换</view>
		</view>
		
		<view class="exchangeBtn1 margin50" @click="show=true">红包兑换工分</view>
		<u-popup v-model="show" mode="center">
			<view class="popVue padding40">
				<view class="title">红包兑换工分</view>
				<view class="balance">公分余额： {{ user.point }}</view>
				<view class="inpVue margin30 flex align-center">
					<view style="line-height: 1; width: 180rpx;">兑换数量</view>
					<input type="number" v-model="quantity"/>
				</view>
				<view class="btn margin40" @click="submitFun()">确定</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		data(){
			return {
				statusBarHeight: 0,
				barHeight: 0,
				show: false,
				nums: [],
				numId: "",
				num: 0,
				quantity: ""
			}
		},
		
		computed: {
			...mapState({
				user: state=> state.user
			})
		},
		onLoad() {
			let that = this;
			wx.getSystemInfo({
				success(res) {
					let deviceWidth = wx.getSystemInfoSync().windowWidth
					that.statusBarHeight = Number(res.statusBarHeight) * ( 750 / deviceWidth ) 
					let menuButtonInfo = wx.getMenuButtonBoundingClientRect()
					let barHeight = menuButtonInfo.height + (menuButtonInfo.top - res.statusBarHeight) * 2
					that.barHeight = Number(barHeight) * ( 750 / deviceWidth )
				},
			});
		},
		async onShow(){
			const res = await this.$request("/api/WeChat/numList", {},"GET")
			if(res.code===200){
				this.nums = res.data
				this.numId = res.data[0].id
				this.num = res.data[0].num
			}
		},
		
		
		methods: {
			async submit(){
				if(this.user.point<this.num) this.$u.toast("余额不足")
				const res = await this.$request(`/api/WeChat/withdrawRedPacketOrder?exId=${this.numId}`, {
					exId: this.numId
				}, "POST")
				if(res.code===200){
					this.$u.toast("操作成功")
					this.$store.dispatch("getUser")
				}else{
					this.$u.toast(res.msg)
				}
			},
			
			async submitFun(){
				if(this.user.point<this.num) this.$u.toast("余额不足")
				if(!this.quantity || this.quantity==0) this.$u.toast("请输入正确数量")
				const res = await this.$request(`/api/WeChat/exchangeWorkPoint?number=${this.quantity}`, {
					number: this.quantity
				}, "POST")
				if(res.code===200){
					this.$u.toast("操作成功")
					this.$store.dispatch("getUser")
					this.show = false
					this.quantity = ""
				}else{
					this.$u.toast(res.msg)
				}
			}
		}
	}
</script>

<style lang="less" scoped>
	.popVue {
		width: 690rpx;
		.title {
			text-align: center;
			font-weight: 600;
			font-size: 32rpx;
			color: #333;
		}
		.balance {
			color: #454545;
			padding-top: 30rpx;
			text-align: center;
		}
		input {
			flex: 1;
			height: 80rpx;
			border: 1rpx solid #f5f5f5;
			border-radius: 10rpx;
			padding-left: 20rpx;
		}
		.btn {
			width: 400rpx;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background: #D37430;
			border-radius: 15rpx;
			color: #fff;
		}
	}
	.topVue {
		width: 100%;
		background: #fff;
		position: fixed;
		left: 0;
		top: 0;
		z-index: 99;
		.safeVue {
			width: 100%;
		}
		.opBtn {
			width: 150rpx;
			height: 66rpx;
			border-radius: 10rpx;
			background: #D37430;
			line-height: 66rpx;
			text-align: center;
			color: #fff;
			margin-left: 40rpx;
		}
	}
	
	.box {
		width: 690rpx;
		background: #fff;
		border-radius: 20rpx;
		box-shadow: 0rpx 0rpx 16rpx 0rpx rgba(178, 178, 178, 0.25), inset 0rpx 8rpx 8rpx 0rpx rgba(255, 255, 255, 0.25);
		
		.exchangeBtn {
			width: 520rpx;
			height: 80rpx;
			border-radius: 10rpx;
			background: #D37430;
			line-height: 80rpx;
			text-align: center;
			color: #fff;
		}
	}
	
	.exchangeBtn1 {
		width: 520rpx;
		height: 80rpx;
		border-radius: 10rpx;
		background: #D37430;
		line-height: 80rpx;
		text-align: center;
		color: #fff;
		
	}
	
	.balance {
		color: #454545;
		padding-top: 30rpx;
		text-align: center;
	}
	
	.li {
		width: 150rpx;
		height: 80rpx;
		background: #f5f5f5;
		border-radius: 15rpx;
		line-height: 80rpx;
		text-align: center;
		margin: 0rpx 20rpx 20rpx 20rpx;
	}
	.active {
		background: #D37430;
		color: #fff;
	}
</style>